package org.jivesoftware.smack;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Registration;
import org.jivesoftware.smack.util.StringUtils;

public class AccountManager
{
  private boolean accountCreationSupported;
  private Connection connection;
  private Registration info = null;

  public AccountManager(Connection paramConnection)
  {
    this.accountCreationSupported = false;
    this.connection = paramConnection;
  }

  /**
   * @deprecated
   */
  private void getRegistrationInfo()
    throws XMPPException
  {
    IQ localIQ;
    monitorenter;
    try
    {
      AndFilter localAndFilter;
      PacketCollector localPacketCollector;
      Registration localRegistration = new Registration();
      localRegistration.setTo(this.connection.getServiceName());
      PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
      arrayOfPacketFilter[0] = new PacketIDFilter(localRegistration.getPacketID());
      arrayOfPacketFilter[1] = new PacketTypeFilter(IQ.class);
      throw new XMPPException("No response from server.");
    }
    finally
    {
      monitorexit;
      throw localObject;
      if (localIQ.getType() == IQ.Type.ERROR)
        throw new XMPPException(localIQ.getError());
      this.info = ((Registration)localIQ);
      monitorexit;
    }
  }

  public void changePassword(String paramString)
    throws XMPPException
  {
    Registration localRegistration = new Registration();
    localRegistration.setType(IQ.Type.SET);
    localRegistration.setTo(this.connection.getServiceName());
    HashMap localHashMap = new HashMap();
    localHashMap.put("username", StringUtils.parseName(this.connection.getUser()));
    localHashMap.put("password", paramString);
    localRegistration.setAttributes(localHashMap);
    PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
    arrayOfPacketFilter[0] = new PacketIDFilter(localRegistration.getPacketID());
    arrayOfPacketFilter[1] = new PacketTypeFilter(IQ.class);
    AndFilter localAndFilter = new AndFilter(arrayOfPacketFilter);
    PacketCollector localPacketCollector = this.connection.createPacketCollector(localAndFilter);
    this.connection.sendPacket(localRegistration);
    IQ localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from server.");
    if (localIQ.getType() == IQ.Type.ERROR)
      throw new XMPPException(localIQ.getError());
  }

  public void createAccount(String paramString1, String paramString2)
    throws XMPPException
  {
    if (!(supportsAccountCreation()))
      throw new XMPPException("Server does not support account creation.");
    HashMap localHashMap = new HashMap();
    Iterator localIterator = getAccountAttributes().iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        createAccount(paramString1, paramString2, localHashMap);
        return;
      }
      localHashMap.put((String)localIterator.next(), "");
    }
  }

  public void createAccount(String paramString1, String paramString2, Map<String, String> paramMap)
    throws XMPPException
  {
    if (!(supportsAccountCreation()))
      throw new XMPPException("Server does not support account creation.");
    Registration localRegistration = new Registration();
    localRegistration.setType(IQ.Type.SET);
    localRegistration.setTo(this.connection.getServiceName());
    paramMap.put("username", paramString1);
    paramMap.put("password", paramString2);
    localRegistration.setAttributes(paramMap);
    PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
    arrayOfPacketFilter[0] = new PacketIDFilter(localRegistration.getPacketID());
    arrayOfPacketFilter[1] = new PacketTypeFilter(IQ.class);
    AndFilter localAndFilter = new AndFilter(arrayOfPacketFilter);
    PacketCollector localPacketCollector = this.connection.createPacketCollector(localAndFilter);
    this.connection.sendPacket(localRegistration);
    IQ localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from server.");
    if (localIQ.getType() == IQ.Type.ERROR)
      throw new XMPPException(localIQ.getError());
  }

  public void deleteAccount()
    throws XMPPException
  {
    if (!(this.connection.isAuthenticated()))
      throw new IllegalStateException("Must be logged in to delete a account.");
    Registration localRegistration = new Registration();
    localRegistration.setType(IQ.Type.SET);
    localRegistration.setTo(this.connection.getServiceName());
    new HashMap().put("remove", "");
    PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
    arrayOfPacketFilter[0] = new PacketIDFilter(localRegistration.getPacketID());
    arrayOfPacketFilter[1] = new PacketTypeFilter(IQ.class);
    AndFilter localAndFilter = new AndFilter(arrayOfPacketFilter);
    PacketCollector localPacketCollector = this.connection.createPacketCollector(localAndFilter);
    this.connection.sendPacket(localRegistration);
    IQ localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from server.");
    if (localIQ.getType() == IQ.Type.ERROR)
      throw new XMPPException(localIQ.getError());
  }

  public String getAccountAttribute(String paramString)
  {
    String str1;
    try
    {
      if (this.info == null)
        getRegistrationInfo();
      String str2 = (String)this.info.getAttributes().get(paramString);
      str1 = str2;
      return str1;
    }
    catch (XMPPException localXMPPException)
    {
      localXMPPException.printStackTrace();
      str1 = null;
    }
  }

  public Collection<String> getAccountAttributes()
  {
    Set localSet1;
    try
    {
      if (this.info == null)
        getRegistrationInfo();
      Map localMap = this.info.getAttributes();
      if (localMap != null)
      {
        Set localSet2 = Collections.unmodifiableSet(localMap.keySet());
        localSet1 = localSet2;
        return localSet1;
      }
    }
    catch (XMPPException localXMPPException)
    {
      localXMPPException.printStackTrace();
      localSet1 = Collections.emptySet();
    }
  }

  public String getAccountInstructions()
  {
    String str1;
    try
    {
      if (this.info == null)
        getRegistrationInfo();
      String str2 = this.info.getInstructions();
      str1 = str2;
      return str1;
    }
    catch (XMPPException localXMPPException)
    {
      str1 = null;
    }
  }

  void setSupportsAccountCreation(boolean paramBoolean)
  {
    this.accountCreationSupported = paramBoolean;
  }

  public boolean supportsAccountCreation()
  {
    boolean bool;
    if (this.accountCreationSupported)
    {
      bool = true;
      return bool;
    }
    try
    {
      while (true)
      {
        if (this.info == null)
        {
          getRegistrationInfo();
          if (this.info.getType() == IQ.Type.ERROR)
            break;
          i = 1;
          this.accountCreationSupported = i;
        }
        bool = this.accountCreationSupported;
      }
      int i = 0;
    }
    catch (XMPPException localXMPPException)
    {
      while (true)
        bool = false;
    }
  }
}